/*
 *  Filename:      gsl_debug.c
 *  Author:         Erick Huang<erickhuang1989@gmail.com>
 *  Revised:       2013-06-10
 *  Description:  
 *                      Some functions for debuging .
 *
 *  Copyright (c) Erick Huang. All rights reserved.
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include "gsl_defs.h"
#include "gsl_memory.h"
#include "gsl_string.h"
#include "gsl_debug.h"

#if	defined(WIN32) || defined(_WIN32_WCE)
#define ENDLINE "\r\n"
#else
#define ENDLINE "\n"
#endif


static FILE  *__log_file = NULL;
UINT32 __gsl_log_mask = GSL_LOGLEV_WARNING|GSL_LOGLEV_ERROR|GSL_LOGLEV_FATAL;



void gsl_set_log_file(FILE *file)
{
	__log_file = file;
}

void gsl_set_log_level_mask(UINT32 levelmask)
{
	__gsl_log_mask = levelmask;
}

void __gsl_logv_out(GslLogLevel lev, const char *fmt, va_list args)
{
	const char *lname="undef";
	char *msg;
	
	if (__log_file == NULL) 
		__log_file = stderr;
	
	switch (lev) {
		case GSL_LOGLEV_DEBUG:
			lname="debug";
			break;
		case GSL_LOGLEV_MESSAGE:
			lname="message";
			break;
		case GSL_LOGLEV_WARNING:
			lname="warning";
			break;
		case GSL_LOGLEV_ERROR:
			lname="error";
			break;
		case GSL_LOGLEV_FATAL:
			lname="fatal";
			break;
		default:
			/* you should not reach here */
			gsl_fatal("Bad level !");
			break;
	}
	msg = gsl_strdup_vprintf(fmt, args);

	fprintf(__log_file, "gsl-%s-%s"ENDLINE, lname, msg);
	fflush(__log_file);
	gsl_mem_free(msg);
}


